//
//  OEPaymentSettingViewController.m
//  OneEightyNine
//
//  Created by apple on 2019/3/5.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OEPaymentSettingViewController.h"
#import "OESetpwdViewController.h"
#import "OESetpwdViewController.h"
@interface OEPaymentSettingViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;/**<  */
@property(nonatomic, assign) UISwitch                     *swicthView;/**< <##> */
@end

@implementation OEPaymentSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = OESTRING(@"PAYMENT  SETTING");
    
    NSString * type = @"Fingerprint";
    if (@available(iOS 11.0, *)) {
        if (UIApplication.sharedApplication.delegate.window.safeAreaInsets.bottom) {
            type = @"Face";
        }
    } else {
        // Fallback on earlier versions
    }
    self.dataSouce = NSMutableArray.new;
    ({
        STTableEasyModel * model = [[STTableEasyModel alloc] initWithTextString:@"Payment password" detailString:@""];
        model.obj_id = @"for payment and other validation";
        [self.dataSouce addObject:model];
    });
    ({
        STTableEasyModel * model = [[STTableEasyModel alloc] initWithTextString:[NSString stringWithFormat:@"%@ payment",type] detailString:@""];
        model.obj_id = [NSString stringWithFormat:@"After opening,you can use %@ to pay quickly",type];
        UISwitch * switchView = UISwitch.new;
        BOOL on = [NSUserDefaults.standardUserDefaults boolForKey:OE_FACE_OR_FINGER_PAY_OPEN_KEY];
        [switchView setOn:on];
        model.accessoryView = switchView;
        [switchView addTarget:self action:@selector(switchViewDidChange) forControlEvents:UIControlEventValueChanged];
        self.swicthView = switchView;
        [self.dataSouce addObject:model];
    });
    [self.tableView reloadData];
    
    // Do any additional setup after loading the view.
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return  50;
}
- (NSString*)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    STTableEasyModel * model = self.dataSouce[section];
    return model.obj_id;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    STTableEasyModel * model = self.dataSouce[indexPath.section];
    [cell setSt_tableEasyModel:model];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:OESetpwdViewController.new animated:YES];
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)switchViewDidChange{
    if (!STUserManger.defult.loginedUser.pay_password.length) {
        [self.swicthView setOn:NO];
        [self.navigationController pushViewController:OESetpwdViewController.new animated:YES];
        return;
    }
    if (self.swicthView.isOn) {
        [TMUtils showFaceOrFingerPayType:@"1" handle:^{
            [[NSUserDefaults standardUserDefaults] setBool:self.swicthView.isOn forKey:OE_FACE_OR_FINGER_PAY_OPEN_KEY];
            [[NSUserDefaults standardUserDefaults] synchronize];
            
        } inputPwdHandle:^{
            
        } errorHandle:^{
            [self.swicthView setOn:NO];
        }];
    }else{
        [[NSUserDefaults standardUserDefaults] setBool:self.swicthView.isOn forKey:OE_FACE_OR_FINGER_PAY_OPEN_KEY];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }

}
@end
